package com.hotelsystem.service.impl;

import com.hotelsystem.dao.EmployeeMapper;
import com.hotelsystem.pojo.Employee;
import com.hotelsystem.service.EmployeeService;
import com.hotelsystem.util.Tool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;


@Service(value = "employeeServiceImpl")
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<Employee> getAllEmployeeByEmployee(Employee employee) throws Exception {
        return employeeMapper.getAllEmployee(employee);
    }

    @Override
    public void deleteEmpInfoByName(String empName) throws Exception {
        employeeMapper.deleteEmpInfoByName(empName);
    }

    @Override
    public void addEmpInfo(Employee employee,String imgBase64) throws Exception {
        //假数据 真实数据通过会话获得
        //employee.setCreator("admin");
        //获得当前系统时间
        employee.setCreateDate(new Date());
        if (imgBase64!=null){
            File upload = createRoomFolder();

            System.out.println("upload url:"+upload.getAbsolutePath());
            String  absolutePath = upload.getAbsolutePath();
            String fileName = Tool.createUUID() + ".png";

            String fullPath =  absolutePath+"/"+fileName;
            System.out.println("fullPath"+fullPath);
            try {
                Tool.writeImgBase64ToFile(imgBase64, fullPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            employee.setHeadImage(fileName);
        }else if(imgBase64 == null){
            employee.setHeadImage("empImage.png");
        }
        employeeMapper.addEmpInfo(employee);
    }



    @Override
    public void updateEmpInfoByName(Employee employee,String imgBase64) throws Exception {
        //假数据 真实数据通过会话获得
        //employee.setCreator("admin");
        //获得当前系统时间
        employee.setCreateDate(new Date());
        Employee emp =  employeeMapper.getEmpInfoByuserName(employee.getUsername());
        //如果图片有更改先删除原来的图片
        if (imgBase64 != null && imgBase64 !=""){
            //先删除原来的图片

            try {
                //通过Id查询出原来的Img
                String fileImg = emp.getHeadImage();
                //这里给完整路径，不然别人不知道哪里删除
                File upload = createRoomFolder();
                String fullPath = upload.getAbsolutePath()+"/"+fileImg;
                //先删除原来的图片
                if (fileImg!=null)
                    deleteFile(fullPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //然后在添加现有的图片

            File upload = createRoomFolder();

            System.out.println("upload url:"+upload.getAbsolutePath());
            String fileName = Tool.createUUID() + ".png";
            String fullPath = upload.getAbsolutePath()+"/"+fileName;
            try {
                Tool.writeImgBase64ToFile(imgBase64, fullPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //再添加现有的个图片
            employee.setHeadImage(fileName);
        }else {
            //还是添加原来的图片
            employee.setHeadImage(emp.getHeadImage());
        }

        employeeMapper.updateEmpInfoByName(employee);
    }
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public Employee getEmpInfoByuserName(String username) throws Exception {
        return employeeMapper.getEmpInfoByuserName(username);
    }
    /**
     * 刪除单个文件 如果文件存在就删除，如果不存在就不删
     * @param fileName
     */
    public  void deleteFile(String fileName) {
        File f =  createRoomFolder();
        String fullPath = f.getAbsolutePath()+"/"+fileName;
        File file = new File(fullPath);
        //这里判断文件是否存在
        if (fileName!=null && file.exists() && file.isFile()){
            try {
                if (file.delete()) {
                    System.out.println(file.getName() + "file is deleted");
                } else {
                    System.out.println("file Delete failed.");
                }
            } catch (Exception e) {
                System.out.println("Exception occured for file Delete");
                e.printStackTrace();
            }
        }
    }

    private File createRoomFolder(){
        //获取根路径的方法
        File path = new File(System.getProperty("user.dir"));
        File upload = new File(path.getAbsolutePath(),"static/upload/empImgs");
        if(!upload.exists()) upload.mkdirs();
        return upload;
    }

    @Override
    public Employee login(String username) throws Exception {
        //调用方法
        Employee emp = employeeMapper.getEmpInfoByuserName(username);
        return emp;//失败
    }

    /**
     * 修改密码
     * @param empId
     * @param newpassword
     * @throws Exception
     */
    @Override
    public void update(Integer empId, String newpassword) throws Exception {
        employeeMapper.update(empId,newpassword);
    }
}
